<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class staff extends template_plugin
{
	var $ipsclass;
	
	var $name = "Staff Management";
	var $multiple = 1;
	
	var $settings = array();
	
	function _load(&$plugin)
	{
		parent::_load($plugin);
		
		if($plugin['link'])
		{
			$this->settings = unserialize($plugin['plugin_config']);
		}
		else
		{
			$this->settings['fine_member'] = 1;
			$this->settings['edit_money'] = 1;
			$this->settings['edit_inventory'] = 1;
			$this->settings['add_inventory'] = 1;
			$this->settings['protected'][] = $this->ipsclass->vars['admin_group'];
		}
	}
	
	function acp_settings()
	{
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $gid => $group)
		{
			$groups[] = array($gid, $group['g_title']);
		}
		
		$html = "";
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow to fine a member money?</b><br />This option will not allow fining a negative amount",
															$this->ipsclass->adskin->form_yes_no('fine_member',$this->settings['fine_member']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow to edit a member money?</b><br />This option will allow you set more and less money.",
															$this->ipsclass->adskin->form_yes_no('edit_money',$this->settings['edit_money']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow to edit a member inventory?</b><br />This includes editing the resell price and price to the items as well as deleting them.",
															$this->ipsclass->adskin->form_yes_no('edit_inventory',$this->settings['edit_inventory']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow to add to a member inventory?</b>",
															$this->ipsclass->adskin->form_yes_no('add_inventory',$this->settings['add_inventory']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which groups are protected from this plugin?</b>",
															$this->ipsclass->adskin->form_multiselect("protected[]", $groups, $this->settings['protected']),
													)	);
		
		return $html;
	}
	
	function acp_save_settings(&$input)
	{
		$save = array();
		$save['fine_member'] = max(min($this->ipsclass->input['fine_member'],1),0);
		$save['edit_money'] = max(min($this->ipsclass->input['edit_money'],1),0);
		$save['edit_inventory'] = max(min($this->ipsclass->input['edit_inventory'],1),0);
		$save['add_inventory'] = max(min($this->ipsclass->input['add_inventory'],1),0);
		$save['protected'] = is_array($this->ipsclass->input['protected']) ? $this->ipsclass->input['protected'] : array();
		$input['plugin_config'] = serialize($save);
	}
	
	function acp()
	{
		$this->ipsclass->admin->show_inframe($this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?autocom=market&plugin=".$this->plugin['link']);
	}
	
	function run()
	{
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->plugin['name']."</a>");
		$this->ipsclass->subtitle = "";
		
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		$this->ipsclass->market_class->title[] = array($this->plugin['name'],'market_'.$this->plugin['link']);
		if(!in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->Error(array('MSG'=>'staff_need_permission'));
		}
		
		if(!$this->settings['fine_member'] && !$this->settings['edit_money'] && !$this->settings['edit_inventory'] && !$this->settings['add_inventory'])
		{
			$this->ipsclass->Error(array('MSG'=>'staff_no_action'));
		}
		
		$do_redirect = array(
								'form' => 'staff_form',
								'action' => 'action_form',
								'apply' => 'apply_action',
							  );
		
		if(!isset($do_redirect[$this->ipsclass->input['do']]))
		{
			$this->ipsclass->input['do'] = 'form';
		}
		
		$func = 'run_'.$do_redirect[$this->ipsclass->input['do']];
		$this->$func();
	}
	
	function run_staff_form()
	{
		$this->ipsclass->lang['staff_edit_member_money'] = sprintf($this->ipsclass->lang['staff_edit_member_money'],$this->ipsclass->vars['market_currency']);
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->staff_find_member($this->settings);
	}
	
	function run_action_form()
	{
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		
		if(!$member || !$member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_such_user'));
		}
		
		if(in_array($member['mgroup'],$this->settings['protected']))
		{
			$this->ipsclass->Error(array('MSG'=>'staff_protected_user'));
		}
		
		$redirect = array(
							'fine' => 'fine_member_money',
							'money' => 'edit_member_money',
							'edit' => 'edit_member_inventory',
							'add' => 'add_member_inventory',
						);
		
		if(!isset($redirect[$this->ipsclass->input['action']]))
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$func = 'action_'.$redirect[$this->ipsclass->input['action']];
		$this->$func($member);
	}
	
	function action_fine_member_money($member)
	{
		if(!$this->settings['fine_member'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->staff_fine_member_money($member);
	}
	
	function action_edit_member_money($member)
	{
		if(!$this->settings['edit_money'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$this->ipsclass->lang['staff_member_money'] = sprintf($this->ipsclass->lang['staff_member_money'],$this->ipsclass->vars['market_currency']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->staff_edit_member_money($member);
	}
	
	function action_edit_member_inventory($member)
	{
		if(!$this->settings['edit_inventory'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$inventory = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"member_id=".$member['id']));
		$this->ipsclass->DB->exec_query();
		while($inv = $this->ipsclass->DB->fetch_row())
		{
			$inv['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$inv['item']]->parse_single_description($inv)));
			$inventory[$inv['i_id']] = $inv;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->staff_edit_member_inventory($member,$inventory);
	}
	
	function action_add_member_inventory($member)
	{
		if(!$this->settings['add_inventory'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$items = array();
		$null = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			if(!$this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($null,1))
			{
				continue;
			}
			
			$item['expiring'] = $item['expire_time'] == 0 ? "<i>".$this->ipsclass->lang['never']."</i>" : $this->ipsclass->numbers->time_string($item['expire'],'TINY');
			$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item_id']]->parse_store_description()));
			$items[] = $item;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->staff_edit_member_inventory($member,$items);
	}
	
	function run_apply_action()
	{
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$this->ipsclass->input['member_id']));
		
		if(!$member || !$member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_such_user'));
		}
		
		if(in_array($member['mgroup'],$this->settings['protected']))
		{
			$this->ipsclass->Error(array('MSG'=>'staff_protected_user'));
		}
		
		$redirect = array(
							'fine' => 'fine_member_money',
							'money' => 'edit_member_money',
							'edit' => 'edit_member_inventory',
							'add' => 'add_member_inventory',
						);
		
		if(!isset($redirect[$this->ipsclass->input['action']]))
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$func = 'apply_'.$redirect[$this->ipsclass->input['action']];
		$this->$func($member);
	}
	
	function apply_fine_member_money($member)
	{
		if(!$this->settings['fine_member'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$this->ipsclass->DB->do_update('members',array($this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->parse_input_number($this->ipsclass->input['amount'],'int'))),"id=".$member['id']);
		
		$this->ipsclass->market_class->redirect('staff_member_fined',"plugin=".$this->ipsclass->input['plugin']);
	}
	
	function apply_edit_member_money($member)
	{
		if(!$this->settings['edit_money'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		
		$this->ipsclass->DB->do_update('members',array($this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['amount'],'int')),"id=".$member['id']);
		
		$this->ipsclass->market_class->redirect('staff_member_money_edited',"plugin=".$this->ipsclass->input['plugin']);
	}
	
	function apply_edit_member_inventory($member)
	{
		if(!$this->settings['edit_inventory'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"member_id=".$member['id']));
		$q = $this->ipsclass->DB->exec_query();
		while($inv = $this->ipsclass->DB->fetch_row($q))
		{
			if($this->ipsclass->input['delete'][$inv['i_id']])
			{
				$this->ipsclass->DB->do_delete('market_inventory',"i_id=".$inv['i_id']);
			}
			else
			{
				$update = array();
				$update['price_bought'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['inv'][$inv['i_id']]['price_bought'],'int');
				$update['resell_price'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['inv'][$inv['i_id']]['resell_price'],'int');
				if($update['price_bought'] != $inv['price_bought'] || $update['resell_price'] != $inv['resell_price'])
				{
					$this->ipsclass->DB->do_update('market_inventory',$update,"i_id=".$inv['i_id']);
				}
			}
		}
		
		$this->ipsclass->market_class->redirect('staff_member_inventory_edited',"plugin=".$this->ipsclass->input['plugin']);
	}
	
	function apply_add_member_inventory($member)
	{
		if(!$this->settings['add_inventory'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$time = time();
		
		foreach(array_keys($this->ipsclass->cache['market_items']) as $item_id)
		{
			$this->ipsclass->input['inv'][$item_id]['price_bought'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['inv'][$item_id]['price_bought'],'int');
			$this->ipsclass->input['inv'][$item_id]['resell_price'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['inv'][$item_id]['resell_price'],'int');
			$this->ipsclass->input['quan'][$item_id] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['quan'][$item_id],'int');
			if($this->ipsclass->input['quan'][$item_id] <= 0)
			{
				continue;
			}
			
			for($i = 0; $i < $this->ipsclass->input['quan'][$item_id]; $i++)
			{
				$inv_item = array(
									'member_id' => $member['id'],
									'item' => $item_id,
									'price_bought' => $this->ipsclass->input['inv'][$item_id]['price_bought'],
									'resell_price' => $this->ipsclass->input['inv'][$item_id]['resell_price'],
									'time_bought' => $time,
									'settings' => '',
								);
				if($this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($inv_item,1))
				{
					$this->ipsclass->DB->do_insert('market_inventory',$inv_item);
				}
			}
		}
		
		$this->ipsclass->market_class->redirect('staff_member_inventory_added',"plugin=".$this->ipsclass->input['plugin']);
	}
}
?>